﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System;
using System.Threading.Tasks;

namespace SCM.Manage.Middleware
{
    public class ModelStateMiddleware
    {
        private RequestDelegate next;
        public ModelStateMiddleware(RequestDelegate next)
        {
            this.next = next;
        }
        public async Task Invoke(HttpContext context)
        {
            var ModelState = context.Features.Get<ModelStateFeature>()?.ModelState;
            if (ModelState == null)
            {
                await next(context);   
                return;
            }
            if (!ModelState.IsValid)
            {
                Console.WriteLine(ModelState.Values.ToString());
            }
            await next(context);
        }
    }

    internal class ModelStateFeature
    {
        public ModelStateDictionary ModelState { get; set; }

        public ModelStateFeature(ModelStateDictionary state)
        {
            this.ModelState = state;
        }
    }
}
